package com.topc.others.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.model.UserAuthDto;
import com.topc.others.model.dto.SaveCompetitionComposeDto;
import com.topc.others.model.dto.SaveCompetitionInfoDto;
import com.topc.others.model.po.CompetitionInfo;
import com.topc.others.model.vo.CompetitionApplyShowVo;
import com.topc.others.model.vo.CompetitionDetailShowVo;
import com.topc.others.model.vo.CompetitionInfoShowVo;
import com.topc.others.model.vo.CompetitionRankingShowVo;
import com.topc.others.service.ICompetitionInfoService;
import com.topc.others.service.ICompetitionUserMidService;
import com.topc.others.service.impl.CompetitionInfoServiceImpl;
import com.topc.others.util.SecurityUtil;
import io.swagger.models.auth.In;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/close/competition")
public class CompetitionCloseController {

    @Autowired
    private ICompetitionInfoService competitionInfoService;

    @Autowired
    private ICompetitionUserMidService competitionUserMidService;

    //添加竞赛
    @PostMapping("/save/competition")
    public CommonResult<String> saveCompetitionInfo(@RequestBody @Validated SaveCompetitionInfoDto saveCompetitionInfoDto){
        competitionInfoService.saveCompetitionInfo(saveCompetitionInfoDto);
        return CommonResult.success("添加成功");
    }

    //删除竞赛
    @GetMapping("/delete/competition")
    public CommonResult<String> deleteCompetitionInfoById(@NotNull Long id){
        competitionInfoService.deleteCompetitionInfoById(id);
        return CommonResult.success("删除成功");
    }

    //修改竞赛状态
    @PostMapping("/update/competition")
    public CommonResult<String> updateCompetitionInfoById(@NotNull Long id,@Range(max = 4,min = 0) Integer status) {
        competitionInfoService.updateCompetitionInfoById(id, status);
        return CommonResult.success("修改成功");
    }

    /**
     * TODO
     * @param pageParams
     * @return
     */
    //查看竞赛列表
    @PostMapping("/search-competition")
    public CommonResult<PageResult<CompetitionInfoShowVo>> getCompetitionInfoByPage(PageParams pageParams, String title){
        String userNumber = Objects.requireNonNull(SecurityUtil.getUser()).getNumber();
        PageResult<CompetitionInfoShowVo> pageResult=competitionInfoService.getCompetitionInfoByPage(userNumber,pageParams,title);
        return CommonResult.success(pageResult);
    }

    /**
     * TODO
     * @param competitionId
     * @return
     */
    //获取竞赛详细信息
    @GetMapping("/search-detail-by-id")
    public CommonResult<CompetitionDetailShowVo> getCompetitionDetailInfo(@NotNull Long competitionId){
        String userNumber = SecurityUtil.getUser().getNumber();
        CompetitionDetailShowVo competitionDetailShowVo = competitionInfoService.getDetailById(userNumber,competitionId);
        return CommonResult.success(competitionDetailShowVo);
    }

    //添加报名信息(用户报名)
    @PostMapping("/save/apply")
    public CommonResult<String> saveApplyInfo(@RequestBody @Validated SaveCompetitionComposeDto saveCompetitionComposeDto){
        saveCompetitionComposeDto.setUserNumber(SecurityUtil.getUser().getNumber());
        competitionUserMidService.saveUserInfo(saveCompetitionComposeDto);
        return CommonResult.success("添加成功");
    }

    //取消报名(用户取消报名)
    @PostMapping("/delete/apply")
    public CommonResult<String> deleteApplyInfoById(@NotNull Long id){
        String userNumber = SecurityUtil.getUser().getNumber();
        competitionUserMidService.deleteUserInfoById(id,userNumber);
        return CommonResult.success("删除成功");
    }

    //修改报名状态(修改报名状态)
    @GetMapping("/update/apply")
    public CommonResult<String> updateApplyInfoById(@NotNull Long id){
        String userNumber = SecurityUtil.getUser().getNumber();
        competitionUserMidService.updateApplyInfoById(id,userNumber);
        return CommonResult.success("修改成功");
    }

    /**
     * TODO
     * @param pageParams
     * @param id
     * @return
     */
    //查看竞赛报名信息(管理员查看竞赛报名信息)
    @PostMapping("/search-apply")
    public CommonResult<PageResult<CompetitionApplyShowVo>> getApplyInfoByPage(PageParams pageParams, @NotNull Long id){
        PageResult<CompetitionApplyShowVo> pageResult=competitionUserMidService.getApplyInfoByPage(pageParams,id);
        return CommonResult.success(pageResult);
    }

    //用户查看自身报名状态(用户点进竞赛详情界面的按钮状态显示)
    @GetMapping("/search-apply-person")
    public CommonResult<String> getApplyInfoByNumber(@NotNull Long id){
        String number = SecurityUtil.getUser().getNumber();
        String result = competitionUserMidService.getApplyInfoByUserNumber(id, number);
        return CommonResult.success(result);
    }
    //提交竞赛
    @GetMapping("/update-competition-finished")
    public CommonResult<String> updateCompetitionFinished(@NotNull Long id){
        String userNumber = SecurityUtil.getUser().getNumber();
        competitionUserMidService.updateCompetitionFinished(id,userNumber);
        return CommonResult.success("交卷成功");
    }

    //获取用户排名
    @GetMapping("/search-user-ranking")
    public CommonResult<List<CompetitionRankingShowVo>> getUserRanking(@NotNull Long competitionId){
        List<CompetitionRankingShowVo> rankingList=competitionInfoService.getRanking(competitionId);
        return CommonResult.success(rankingList);
    }

}
